﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Animation
{
    public partial class Form1 : Form
    {
        Point Location1;
        Point Location2;

        Point TargetLocation;

        int FrameDeltaX;
        int FrameDeltaY;

        bool Showing;
        int AnimationDuration; // milliseconds
        int ProgressBarAnimationDuration; // milliseconds

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Location1 = new Point(panel1.Location.X, panel1.Location.Y - panel1.Height);
            Location2 = panel1.Location;

            Showing = true;
            AnimationDuration = 350;
            ProgressBarAnimationDuration = 10 * 1000;
            //ShownLocation = panel1.Location;
            //PixelDeltaPerTick = 20;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ToggleBox();
        }

        void ToggleBox()
        {
            if (Showing)
            {
                progressBar1.Value = 0;
                timerProgress.Stop();
                button1.Text = "show";
                //panel1.Visible = false;
                Showing = false;
                TargetLocation = Location1;
            }
            else
            {
                button1.Text = "hide";
                //panel1.Visible = true;
                Showing = true;
                TargetLocation = Location2;

                timerProgress.Start();
            }

            if (!timerAnimation.Enabled)
            {
                int TotalFrames = AnimationDuration / timerAnimation.Interval;
                int DeltaX = TargetLocation.X - panel1.Location.X;
                int DeltaY = TargetLocation.Y - panel1.Location.Y;

                FrameDeltaX = DeltaX / TotalFrames;
                FrameDeltaY = DeltaY / TotalFrames;

                FirstFrame = true;
                timerAnimation.Start();
            }
            else
            {
                FrameDeltaX *= -1;
                FrameDeltaY *= -1;
            }
        }

        bool FirstFrame = false;
        private void timerAnimation_Tick(object sender, EventArgs e)
        {
            if (FirstFrame)
            {
                FirstFrame = false;
            }
            else
            {
                Point NewLocation = new Point(panel1.Location.X + FrameDeltaX, panel1.Location.Y + FrameDeltaY);

                if ((FrameDeltaX < 0 && NewLocation.X <= TargetLocation.X) ||
                    (FrameDeltaX > 0 && NewLocation.X >= TargetLocation.X) ||
                    (FrameDeltaY < 0 && NewLocation.Y <= TargetLocation.Y) ||
                    (FrameDeltaY > 0 && NewLocation.Y >= TargetLocation.Y))
                {
                    NewLocation = TargetLocation;
                    timerAnimation.Stop();
                }

                panel1.Location = NewLocation;
            }

            
        }

        private void timerProgress_Tick(object sender, EventArgs e)
        {
            //float jump = ((float)timerProgress.Interval / (float)ProgressBarAnimationDuration) * 100;
            //progressBar1.Value += Convert.ToInt32(Math.Ceiling(jump));

            progressBar1.Value += 10;

            if (progressBar1.Value >= 100)
            {
                timerProgress.Stop();
                //ToggleBox();
            }
        }
    }
}
